/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.andkulikov.transitionseverywhere.slice;

import com.andkulikov.transitionseverywhere.slice.util.ResUtil;
import com.transitionseverywhere.*;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import com.andkulikov.transitionseverywhere.ResourceTable;
import ohos.utils.LightweightMap;

import java.util.ArrayList;

public class SlideSample  extends AbilitySlice {
    private AdaptiveBoxLayout mDlViewRoot;
    private Text mTxtAutoTrans;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        ComponentContainer layout = (ComponentContainer) LayoutScatter.getInstance(this).
                parse(ResourceTable.Layout_slice_slide_transition, null, false);
        mDlViewRoot = (AdaptiveBoxLayout) layout.findComponentById(ResourceTable.Id_dlViewRoot);
        Object tagMap = new LightweightMap<String, ArrayList<Transition>>();
        mDlViewRoot.setTag(tagMap);
        mTxtAutoTrans = (Text) layout.findComponentById(ResourceTable.Id_txtAutoTrans);
        Button btnDoMagic = (Button) layout.findComponentById(ResourceTable.Id_btnDoMagic);
        ShapeElement background_color_element = new ShapeElement();
        background_color_element.setShape(ShapeElement.RECTANGLE);
        background_color_element.setRgbColor(new RgbColor(ResUtil.getColor(this, ResourceTable.Color_red)));
        background_color_element.setBounds(0, 0, btnDoMagic.getRight(), btnDoMagic.getBottom());
        btnDoMagic.setBackground(background_color_element);

        btnDoMagic.setClickedListener(new VisibleToggleClickListener() {

            @Override
            protected void changeVisibility(boolean visible) {

                TransitionManager.beginDelayedTransition(mDlViewRoot, new Slide(Gravity.RIGHT));
                mTxtAutoTrans.setVisibility(visible ? Component.VISIBLE : Component.HIDE);
            }
        });
        super.setUIContent(layout);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
